<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/blog/lib/pace/pace-theme-bounce.min.css">
  <script src="/blog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ipvb.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false,"b2t":true,"scrollpercent":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Spring Data JPA 基本使用Spring Data JPA:1Spring Data JPA 是 spring data 项目下的一个模块。提供了一套基于 JPA标准操作数据库的简化方案。底层默认的是依赖 Hibernate JPA 来实现的。  Spring Data JPA 的技术特点:1我们只需要定义接口并集成 Spring Data JPA 中所提供的接口就可以了。不需要编写接">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Data JPA的基本使用">
<meta property="og:url" content="https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/index.html">
<meta property="og:site_name" content="Chenyumeng的博客">
<meta property="og:description" content="Spring Data JPA 基本使用Spring Data JPA:1Spring Data JPA 是 spring data 项目下的一个模块。提供了一套基于 JPA标准操作数据库的简化方案。底层默认的是依赖 Hibernate JPA 来实现的。  Spring Data JPA 的技术特点:1我们只需要定义接口并集成 Spring Data JPA 中所提供的接口就可以了。不需要编写接">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img2018.cnblogs.com/blog/1155586/201907/1155586-20190722164809711-1125847282.jpg">
<meta property="article:published_time" content="2020-04-23T10:01:55.000Z">
<meta property="article:modified_time" content="2020-04-23T15:13:17.443Z">
<meta property="article:author" content="Chenyumeng">
<meta property="article:tag" content="笔记">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img2018.cnblogs.com/blog/1155586/201907/1155586-20190722164809711-1125847282.jpg">

<link rel="canonical" href="https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>Spring Data JPA的基本使用 | Chenyumeng的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <link rel="stylesheet" href="/dist/css/share.min.css">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chenyumeng的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">热爱自己，热爱生活</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">29</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">1</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">32</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="search-pop-overlay">
  <div class="popup search-popup">
      <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

  </div>
</div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/chen-yumeng" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/touxiang.JPG">
      <meta itemprop="name" content="Chenyumeng">
      <meta itemprop="description" content="用来记录自己学习中所遇到的问题以及如何解决和自己所学知识的理解">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chenyumeng的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring Data JPA的基本使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-04-23 18:01:55 / 修改时间：23:13:17" itemprop="dateCreated datePublished" datetime="2020-04-23T18:01:55+08:00">2020-04-23</time>
            </span>

          
            <span id="/blog/2020/04/23/SpringDataJPA/" class="post-meta-item leancloud_visitors" data-flag-title="Spring Data JPA的基本使用" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>26k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>24 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Spring-Data-JPA-基本使用"><a href="#Spring-Data-JPA-基本使用" class="headerlink" title="Spring Data JPA 基本使用"></a>Spring Data JPA 基本使用</h1><h2 id="Spring-Data-JPA"><a href="#Spring-Data-JPA" class="headerlink" title="Spring Data JPA:"></a>Spring Data JPA:</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Spring Data JPA 是 spring data 项目下的一个模块。提供了一套基于 JPA标准操作数据库的简化方案。底层默认的是依赖 Hibernate JPA 来实现的。</span><br></pre></td></tr></table></figure>

<h2 id="Spring-Data-JPA-的技术特点"><a href="#Spring-Data-JPA-的技术特点" class="headerlink" title="Spring Data JPA 的技术特点:"></a>Spring Data JPA 的技术特点:</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">我们只需要定义接口并集成 Spring Data JPA 中所提供的接口就可以了。不需要编写接口实现类。</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<p>（注：本文章转载自<a href="https://www.cnblogs.com/chenglc/p/11226693.html" target="_blank" rel="noopener">https://www.cnblogs.com/chenglc/p/11226693.html</a> ，只用于查阅与学习，如需删除请与我联系）</p>
<h1 id="一、-创建SpringDataJPA项目"><a href="#一、-创建SpringDataJPA项目" class="headerlink" title="一、 创建SpringDataJPA项目"></a>一、 创建SpringDataJPA项目</h1><h2 id="1-导入依赖"><a href="#1-导入依赖" class="headerlink" title="1 导入依赖"></a>1 导入依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-jpa<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="2-配置数据源信息"><a href="#2-配置数据源信息" class="headerlink" title="2 配置数据源信息"></a>2 配置数据源信息</h2><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/test</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">root</span></span><br><span class="line"><span class="comment">#spring.jpa.hibernate.ddl-auto</span></span><br><span class="line"><span class="comment">    #create：每次运行程序时，都会重新创建表，故而数据会丢失</span></span><br><span class="line"><span class="comment">    #create-drop：每次运行程序时会先创建表结构，然后待程序结束时清空表</span></span><br><span class="line"><span class="comment">    #upadte：每次运行程序，没有表时会创建表，如果对象发生改变会更新表结构，原有数据不会清空，只会更新（推荐使用）</span></span><br><span class="line"><span class="comment">    #validate：运行程序会校验数据与数据库的字段类型是否相同，字段不同会报错</span></span><br><span class="line"><span class="comment">    #none: 禁用DDL处理</span></span><br><span class="line"><span class="meta">spring.jpa.hibernate.ddl-auto</span>=<span class="string">none</span></span><br><span class="line"><span class="comment">#数据库方言</span></span><br><span class="line"><span class="meta">spring.jpa.database-platform</span>=<span class="string">org.hibernate.dialect.MySQL5Dialect</span></span><br><span class="line"><span class="comment">#显示sql</span></span><br><span class="line"><span class="meta">spring.jpa.show-sql</span>=<span class="string">true</span></span><br><span class="line"><span class="comment">#格式化sql</span></span><br><span class="line"><span class="meta">spring.jpa.properties.hibernate.format_sql</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">spring.jpa.properties.hibernate.type</span>=<span class="string">trace</span></span><br><span class="line"><span class="meta">spring.jpa.properties.hibernate.use_sql_comments</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">spring.jpa.properties.hibernate.jdbc.batch_size</span>=<span class="string">50</span></span><br></pre></td></tr></table></figure>

<h2 id="3-编写Dao"><a href="#3-编写Dao" class="headerlink" title="3 编写Dao"></a>3 编写Dao</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-User"><a href="#4-User" class="headerlink" title="4 User"></a>4 User</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.persistence.Column;</span><br><span class="line"><span class="keyword">import</span> javax.persistence.Entity;</span><br><span class="line"><span class="keyword">import</span> javax.persistence.GeneratedValue;</span><br><span class="line"><span class="keyword">import</span> javax.persistence.GenerationType;</span><br><span class="line"><span class="keyword">import</span> javax.persistence.Id;</span><br><span class="line"><span class="keyword">import</span> javax.persistence.Table;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="meta">@Table</span>(name=<span class="string">"t_users"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Users</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Id</span></span><br><span class="line">    <span class="comment">//strategy=GenerationType.IDENTITY 自增长</span></span><br><span class="line">	<span class="meta">@GeneratedValue</span>(strategy=GenerationType.IDENTITY)</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"userid"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer userid;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"username"</span>)</span><br><span class="line">	<span class="keyword">private</span> String username;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"userage"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer userage;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getUserid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> userid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserid</span><span class="params">(Integer userid)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.userid = userid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getUsername</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUsername</span><span class="params">(String username)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.username = username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getUserage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> userage;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserage</span><span class="params">(Integer userage)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.userage = userage;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Users [userid="</span> + userid + <span class="string">", username="</span> + username + <span class="string">", userage="</span> + userage + <span class="string">"]"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GenericGenerator</span>(name = <span class="string">"idGenerator"</span>, strategy = <span class="string">"uuid"</span>)</span><br><span class="line"><span class="meta">@GeneratedValue</span>(generator = <span class="string">"idGenerator"</span>)</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">主键采用UUID策略</span></span><br><span class="line"><span class="comment">@GenericGenerator是Hibernate提供的主键生成策略注解，注意下面的@GeneratedValue（JPA注解）使用generator = "idGenerator"引用了上面的name = "idGenerator"主键生成策略</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">一般简单的Demo示例中只会使用@GeneratedValue(strategy = GenerationType.IDENTITY)这种主键自增的策略，而实际数据库中表字段主键类型很少是int型的</span></span><br><span class="line"><span class="comment">JPA自带的几种主键生成策略:</span></span><br><span class="line"><span class="comment">TABLE： 使用一个特定的数据库表格来保存主键</span></span><br><span class="line"><span class="comment">SEQUENCE： 根据底层数据库的序列来生成主键，条件是数据库支持序列。这个值要与generator一起使用，generator 指定生成主键使用的生成器（可能是orcale中自己编写的序列）</span></span><br><span class="line"><span class="comment">IDENTITY： 主键由数据库自动生成（主要是支持自动增长的数据库，如mysql）</span></span><br><span class="line"><span class="comment">AUTO： 主键由程序控制，也是GenerationType的默认值</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="5-编写测试代码"><a href="#5-编写测试代码" class="headerlink" title="5 编写测试代码"></a>5 编写测试代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UsersDaoImplTest</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 添加用户</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="meta">@Transactional</span><span class="comment">// 在测试类对于事务提交方式默认的是回滚。</span></span><br><span class="line">	<span class="meta">@Rollback</span>(<span class="keyword">false</span>)<span class="comment">//取消自动回滚</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testInsertUsers</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users users = <span class="keyword">new</span> Users();</span><br><span class="line">		users.setUserage(<span class="number">24</span>);</span><br><span class="line">		users.setUsername(<span class="string">"张三"</span>);</span><br><span class="line">		<span class="keyword">this</span>.usersDao.save(users);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="二、-Spring-Data-JPA-的接口继承结构"><a href="#二、-Spring-Data-JPA-的接口继承结构" class="headerlink" title="二、 Spring Data JPA 的接口继承结构"></a>二、 Spring Data JPA 的接口继承结构</h1><p><img src="https://img2018.cnblogs.com/blog/1155586/201907/1155586-20190722164809711-1125847282.jpg" alt="img"></p>
<h1 id="三、-Spring-Data-JPA-的运行原理"><a href="#三、-Spring-Data-JPA-的运行原理" class="headerlink" title="三、 Spring Data JPA 的运行原理"></a>三、 Spring Data JPA 的运行原理</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PersistenceContext</span>(name=<span class="string">"entityManagerFactory"</span>) </span><br><span class="line"><span class="keyword">private</span> EntityManager em;</span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//org.springframework.data.jpa.repository.support.SimpleJpaRepositor y@fba8bf</span></span><br><span class="line">    <span class="comment">//System.out.println(this.usersDao);</span></span><br><span class="line">    <span class="comment">//class com.sun.proxy.$Proxy29 代理对象 是基于 JDK 的动态代理方式创建的</span></span><br><span class="line">    <span class="comment">//System.out.println(this.usersDao.getClass());</span></span><br><span class="line">    JpaRepositoryFactory factory = <span class="keyword">new</span> JpaRepositoryFactory(em);</span><br><span class="line">    <span class="comment">//getRepository(UsersDao.class);可以帮助我们为接口生成实现类。而 这个实现类是 SimpleJpaRepository 的对象</span></span><br><span class="line">    <span class="comment">//要求:该接口必须要是继承 Repository 接口</span></span><br><span class="line">    UsersDao ud = factory.getRepository(UsersDao<span class="class">.<span class="keyword">class</span>)</span>; </span><br><span class="line">    System.out.println(ud); </span><br><span class="line">    System.out.println(ud.getClass());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="四、-Repository-接口"><a href="#四、-Repository-接口" class="headerlink" title="四、 Repository 接口"></a>四、 Repository 接口</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Repository 接口是 Spring Data JPA 中为我我们提供的所有接口中的顶层接口 Repository 提供了两种查询方式的支持</span><br><span class="line">    1)基于方法名称命名规则查询</span><br><span class="line">    2)基于@Query 注解查询</span><br></pre></td></tr></table></figure>

<h2 id="1-方法名称命名规则查询"><a href="#1-方法名称命名规则查询" class="headerlink" title="1 方法名称命名规则查询"></a>1 方法名称命名规则查询</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">规则:</span><br><span class="line">    findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写)</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>关键字</th>
<th>方法命名</th>
<th>sql where 字句</th>
</tr>
</thead>
<tbody><tr>
<td>And</td>
<td>findByNameAndPwd</td>
<td>where name= ? and pwd =?</td>
</tr>
<tr>
<td>Or</td>
<td>findByNameOrSex</td>
<td>where name= ? or sex=?</td>
</tr>
<tr>
<td>Is,Equal</td>
<td>findById,</td>
<td>findByIdEquals</td>
</tr>
<tr>
<td>Between</td>
<td>findByIdBetween</td>
<td>where id between ? and ?</td>
</tr>
<tr>
<td>LessThan</td>
<td>findByIdLessThan</td>
<td>where id &lt; ?</td>
</tr>
<tr>
<td>LessThanEqual</td>
<td>findByIdLessThanEquals</td>
<td>where id &lt;= ?</td>
</tr>
<tr>
<td>GreaterThan</td>
<td>findByIdGreaterThan</td>
<td>where id &gt; ?</td>
</tr>
<tr>
<td>GreaterThanEqual</td>
<td>findByIdGreaterThanEquals</td>
<td>where id &gt; = ?</td>
</tr>
<tr>
<td>After</td>
<td>findByIdAfter</td>
<td>where id &gt; ?</td>
</tr>
<tr>
<td>Before</td>
<td>findByIdBefore</td>
<td>where id &lt; ?</td>
</tr>
<tr>
<td>IsNull</td>
<td>findByNameIsNull</td>
<td>where name is null</td>
</tr>
<tr>
<td>isNotNull,Not Null</td>
<td>findByNameNotNull</td>
<td>where name is not</td>
</tr>
<tr>
<td>Like</td>
<td>findByNameLike</td>
<td>where name like ?</td>
</tr>
<tr>
<td>NotLike</td>
<td>findByNameNotLike</td>
<td>where name not like ?</td>
</tr>
<tr>
<td>StartingWith</td>
<td>findByNameStartingWith</td>
<td>where name like ‘?%’</td>
</tr>
<tr>
<td>EndingWith</td>
<td>findByNameEndingWith</td>
<td>where name like ‘%?’</td>
</tr>
<tr>
<td>Containing</td>
<td>findByNameContaining</td>
<td>where name like ‘%?%’</td>
</tr>
<tr>
<td>OrderBy</td>
<td>findByIdOrderByXDesc</td>
<td>where id=? order by x desc</td>
</tr>
<tr>
<td>Not</td>
<td>findByNameNot</td>
<td>where name &lt;&gt; ?</td>
</tr>
<tr>
<td>In</td>
<td>findByIdIn(Collection&lt;?&gt; c)</td>
<td>where id in (?)</td>
</tr>
<tr>
<td>NotIn</td>
<td>findByIdNotIn(Collection&lt;?&gt; c)</td>
<td>where id not in (?)</td>
</tr>
<tr>
<td>True</td>
<td>findByAaaTue</td>
<td>where aaa = true</td>
</tr>
<tr>
<td>False</td>
<td>findByAaaFalse</td>
<td>where aaa = false</td>
</tr>
<tr>
<td>IgnoreCase</td>
<td>findByNameIgnoreCase</td>
<td>where UPPER(name)=UPPER(?)</td>
</tr>
</tbody></table>
<h4 id="创建接口"><a href="#创建接口" class="headerlink" title="创建接口"></a>创建接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">Repository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;</span><br><span class="line">	<span class="comment">//方法名称命名规则</span></span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">findByUsernameIs</span><span class="params">(String string)</span></span>;</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">findByUsernameLike</span><span class="params">(String string)</span></span>;</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">findByUsernameAndUserageGreaterThanEqual</span><span class="params">(String name,Integer age)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试类"><a href="#测试类" class="headerlink" title="测试类"></a>测试类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 需求：使用用户名作为查询条件</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">/**</span></span><br><span class="line"><span class="comment">		 * 判断相等的条件，有三种表示方式</span></span><br><span class="line"><span class="comment">		 * 1,什么都不写，默认的就是做相等判断</span></span><br><span class="line"><span class="comment">		 * 2,Is</span></span><br><span class="line"><span class="comment">		 * 3,Equal</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findByUsernameIs(<span class="string">"王五"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 需求：根据用户姓名做Like处理</span></span><br><span class="line"><span class="comment">	 * Like:条件关键字</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findByUsernameLike(<span class="string">"王%"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 需求：查询名称为王五，并且他的年龄大于等于22岁</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findByUsernameAndUserageGreaterThanEqual(<span class="string">"王五"</span>, <span class="number">22</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-基于-Query-注解的查询"><a href="#2-基于-Query-注解的查询" class="headerlink" title="2 基于@Query 注解的查询"></a>2 基于@Query 注解的查询</h2><h3 id="2-1通过-JPQL-语句查询"><a href="#2-1通过-JPQL-语句查询" class="headerlink" title="2.1通过 JPQL 语句查询"></a>2.1通过 JPQL 语句查询</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">JPQL:	</span><br><span class="line">    通过 Hibernate 的 HQL 演变过来的。他和 HQL 语法及其相似。</span><br></pre></td></tr></table></figure>

<h4 id="创建接口-1"><a href="#创建接口-1" class="headerlink" title="创建接口"></a>创建接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">Repository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;	</span><br><span class="line">	<span class="comment">//使用@Query注解查询</span></span><br><span class="line">	<span class="meta">@Query</span>(value=<span class="string">"from Users where username = ?"</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByNameUseJPQL</span><span class="params">(String name)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Query</span>(<span class="string">"from Users where username like ?"</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByLikeNameUseJPQL</span><span class="params">(String name)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Query</span>(<span class="string">"from Users where username = ? and userage &gt;= ?"</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByNameAndAge</span><span class="params">(String name,Integer age)</span></span>;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试类-1"><a href="#测试类-1" class="headerlink" title="测试类"></a>测试类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 JPQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByNameUseJPQL(<span class="string">"王五"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 JPQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByLikeNameUseJPQL(<span class="string">"王%"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 JPQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test6</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByNameAndAge(<span class="string">"王五"</span>, <span class="number">22</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-2通过-SQL-语句查询"><a href="#2-2通过-SQL-语句查询" class="headerlink" title="2.2通过 SQL 语句查询"></a>2.2通过 SQL 语句查询</h2><h4 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">Repository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;</span><br><span class="line">	<span class="comment">//使用@Query注解查询SQL</span></span><br><span class="line">	<span class="comment">//nativeQuery:默认的是false.表示不开启sql查询。是否对value中的语句做转义。</span></span><br><span class="line">	<span class="meta">@Query</span>(value=<span class="string">"select * from t_users where username = ?"</span>,nativeQuery=<span class="keyword">true</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByNameUseSQL</span><span class="params">(String name)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Query</span>(value=<span class="string">"select * from t_users where username like ?"</span>,nativeQuery=<span class="keyword">true</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByLikeNameUseSQL</span><span class="params">(String name)</span></span>;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Query</span>(value=<span class="string">"select * from t_users where username = ? and userage &gt;= ?"</span>,nativeQuery=<span class="keyword">true</span>)</span><br><span class="line">	<span class="function">List&lt;Users&gt; <span class="title">queryUserByNameAndAgeUseSQL</span><span class="params">(String name,Integer age)</span></span>;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试类-2"><a href="#测试类-2" class="headerlink" title="测试类"></a>测试类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 SQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByNameUseSQL(<span class="string">"王五"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 SQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test8</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByLikeNameUseSQL(<span class="string">"王%"</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span>查询 SQL</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test9</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.queryUserByNameAndAgeUseSQL(<span class="string">"王五"</span>, <span class="number">22</span>);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-通过-Query-注解完成数据更新"><a href="#3-通过-Query-注解完成数据更新" class="headerlink" title="3 通过@Query 注解完成数据更新"></a>3 通过@Query 注解完成数据更新</h3><h4 id="接口-1"><a href="#接口-1" class="headerlink" title="接口"></a>接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">Repository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;</span><br><span class="line"><span class="meta">@Query</span>(<span class="string">"update Users set userage = ? where userid = ?"</span>)</span><br><span class="line">	<span class="meta">@Modifying</span> <span class="comment">//@Modifying当前语句是一个更新语句</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">updateUserAgeById</span><span class="params">(Integer age,Integer id)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试类-3"><a href="#测试类-3" class="headerlink" title="测试类"></a>测试类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Repository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 测试<span class="doctag">@Query</span> update</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="meta">@Transactional</span></span><br><span class="line">	<span class="meta">@Rollback</span>(<span class="keyword">false</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test10</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.usersDao.updateUserAgeById(<span class="number">24</span>, <span class="number">5</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="五、-CrudRepository-接口"><a href="#五、-CrudRepository-接口" class="headerlink" title="五、 CrudRepository 接口"></a>五、 CrudRepository 接口</h1><h2 id="1-创建接口"><a href="#1-创建接口" class="headerlink" title="1 创建接口"></a>1 创建接口</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * CrudRepository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">CrudRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt; </span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-测试代码"><a href="#2-测试代码" class="headerlink" title="2 测试代码"></a>2 测试代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * CrudRepository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 添加单条数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users user = <span class="keyword">new</span> Users();</span><br><span class="line">		user.setUserage(<span class="number">21</span>);</span><br><span class="line">		user.setUsername(<span class="string">"赵小丽"</span>);</span><br><span class="line">		<span class="keyword">this</span>.usersDao.save(user);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 批量添加数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users user = <span class="keyword">new</span> Users();</span><br><span class="line">		user.setUserage(<span class="number">21</span>);</span><br><span class="line">		user.setUsername(<span class="string">"赵小丽"</span>);</span><br><span class="line">		</span><br><span class="line">		Users user1 = <span class="keyword">new</span> Users();</span><br><span class="line">		user1.setUserage(<span class="number">25</span>);</span><br><span class="line">		user1.setUsername(<span class="string">"王小虎"</span>);</span><br><span class="line">		</span><br><span class="line">		List&lt;Users&gt; list= <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">		list.add(user);</span><br><span class="line">		list.add(user1);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">this</span>.usersDao.save(list);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 根据ID查询单条数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users users = <span class="keyword">this</span>.usersDao.findOne(<span class="number">13</span>);</span><br><span class="line">		System.out.println(users);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 查询全部数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span></span>&#123;</span><br><span class="line">		List&lt;Users&gt; list = (List&lt;Users&gt;)<span class="keyword">this</span>.usersDao.findAll();</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 删除数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.usersDao.delete(<span class="number">13</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 更新数据 方式一</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test6</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users user = <span class="keyword">this</span>.usersDao.findOne(<span class="number">12</span>);</span><br><span class="line">		user.setUsername(<span class="string">"王小红"</span>);</span><br><span class="line">		<span class="keyword">this</span>.usersDao.save(user);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 更新数据 方式二</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="meta">@Transactional</span></span><br><span class="line">	<span class="meta">@Rollback</span>(<span class="keyword">false</span>)</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users user = <span class="keyword">this</span>.usersDao.findOne(<span class="number">12</span>);<span class="comment">//持久化状态的</span></span><br><span class="line">		user.setUsername(<span class="string">"王小小"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="六、-PagingAndSortingRepository-接口"><a href="#六、-PagingAndSortingRepository-接口" class="headerlink" title="六、 PagingAndSortingRepository 接口"></a>六、 PagingAndSortingRepository 接口</h1><h2 id="1-分页处理"><a href="#1-分页处理" class="headerlink" title="1 分页处理"></a>1 分页处理</h2><h3 id="1-1创建接口"><a href="#1-1创建接口" class="headerlink" title="1.1创建接口"></a>1.1创建接口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * PagingAndSortingRepository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">PagingAndSortingRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt;</span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-2测试代码"><a href="#1-2测试代码" class="headerlink" title="1.2测试代码"></a>1.2测试代码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * CrudRepository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 分页</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> page = <span class="number">2</span>; <span class="comment">//page:当前页的索引。注意索引都是从0开始的。</span></span><br><span class="line">		<span class="keyword">int</span> size = <span class="number">3</span>;<span class="comment">// size:每页显示3条数据</span></span><br><span class="line">		Pageable pageable= <span class="keyword">new</span> PageRequest(page, size);</span><br><span class="line">		Page&lt;Users&gt; p = <span class="keyword">this</span>.usersDao.findAll(pageable);</span><br><span class="line">		System.out.println(<span class="string">"数据的总条数："</span>+p.getTotalElements());</span><br><span class="line">		System.out.println(<span class="string">"总页数："</span>+p.getTotalPages());</span><br><span class="line">		List&lt;Users&gt; list = p.getContent();</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-排序的处理"><a href="#2-排序的处理" class="headerlink" title="2 排序的处理"></a>2 排序的处理</h2><h3 id="2-1测试代码"><a href="#2-1测试代码" class="headerlink" title="2.1测试代码"></a>2.1测试代码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * CrudRepository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">		</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 对单列做排序处理</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//Sort:该对象封装了排序规则以及指定的排序字段(对象的属性来表示)</span></span><br><span class="line">		<span class="comment">//direction:排序规则</span></span><br><span class="line">		<span class="comment">//properties:指定做排序的属性</span></span><br><span class="line">		Sort sort = <span class="keyword">new</span> Sort(Direction.DESC,<span class="string">"userid"</span>);</span><br><span class="line">		List&lt;Users&gt; list = (List&lt;Users&gt;)<span class="keyword">this</span>.usersDao.findAll(sort);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 多列的排序处理</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//Sort:该对象封装了排序规则以及指定的排序字段(对象的属性来表示)</span></span><br><span class="line">		<span class="comment">//direction:排序规则</span></span><br><span class="line">		<span class="comment">//properties:指定做排序的属性</span></span><br><span class="line">		Order order1 = <span class="keyword">new</span> Order(Direction.DESC,<span class="string">"userage"</span>);</span><br><span class="line">		Order order2 = <span class="keyword">new</span> Order(Direction.ASC,<span class="string">"username"</span>);</span><br><span class="line">		Sort sort = <span class="keyword">new</span> Sort(order1,order2);</span><br><span class="line">		List&lt;Users&gt; list = (List&lt;Users&gt;)<span class="keyword">this</span>.usersDao.findAll(sort);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="七、-JpaRepository-接口"><a href="#七、-JpaRepository-接口" class="headerlink" title="七、 JpaRepository 接口"></a>七、 JpaRepository 接口</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JpaRepository 接口是我们开发时使用的最多的接口。其特点是可以帮助我们将其他接口的方法的返回值做适配处理。可以使得我们在开发时更方便的使用这些方法。</span><br></pre></td></tr></table></figure>

<h4 id="创建接口-2"><a href="#创建接口-2" class="headerlink" title="创建接口"></a>创建接口</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;**</span><br><span class="line"> * JpaRepository接口讲解</span><br><span class="line"> * @author Administrator</span><br><span class="line"> *</span><br><span class="line"> *&#x2F;</span><br><span class="line">public interface UsersDao extends JpaRepository&lt;Users, Integer&gt;&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试类-4"><a href="#测试类-4" class="headerlink" title="测试类"></a>测试类</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;**</span><br><span class="line"> * JpaRepository接口测试</span><br><span class="line"> * @author Administrator</span><br><span class="line"> *</span><br><span class="line"> *&#x2F;</span><br><span class="line">@RunWith(SpringJUnit4ClassRunner.class)</span><br><span class="line">@ContextConfiguration(&quot;classpath:applicationContext.xml&quot;)</span><br><span class="line">public class RepositoryTest &#123;</span><br><span class="line"></span><br><span class="line">	@Autowired</span><br><span class="line">	private UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	&#x2F;**</span><br><span class="line">	 * 查询全部数据</span><br><span class="line">	 *&#x2F;</span><br><span class="line">	@Test</span><br><span class="line">	public void test1()&#123;</span><br><span class="line">		List&lt;Users&gt; list  &#x3D; this.usersDao.findAll();</span><br><span class="line">		for (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="八、-JpaSpecificationExecutor-接口"><a href="#八、-JpaSpecificationExecutor-接口" class="headerlink" title="八、 JpaSpecificationExecutor 接口"></a>八、 JpaSpecificationExecutor 接口</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">完成多条件查询，并且支持分页与排序</span><br></pre></td></tr></table></figure>

<h2 id="1-单条件查询"><a href="#1-单条件查询" class="headerlink" title="1 单条件查询"></a>1 单条件查询</h2><h3 id="1-1创建接口-1"><a href="#1-1创建接口-1" class="headerlink" title="1.1创建接口"></a>1.1创建接口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * JpaSpecificationExecutor接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *注意：JpaSpecificationExecutor&lt;Users&gt;:不能单独使用，需要配合着jpa中的其他接口一起使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt;,<span class="title">JpaSpecificationExecutor</span>&lt;<span class="title">Users</span>&gt;</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="测试接口"><a href="#测试接口" class="headerlink" title="测试接口"></a>测试接口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * JpaRepository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 需求：根据用户姓名查询数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">			<span class="comment">/**</span></span><br><span class="line"><span class="comment">			 * <span class="doctag">@return</span> Predicate:定义了查询条件</span></span><br><span class="line"><span class="comment">			 * <span class="doctag">@param</span> Root&lt;Users&gt; root:根对象。封装了查询条件的对象</span></span><br><span class="line"><span class="comment">			 * <span class="doctag">@param</span> CriteriaQuery&lt;?&gt; query:定义了一个基本的查询。一般不使用</span></span><br><span class="line"><span class="comment">			 * <span class="doctag">@param</span> CriteriaBuilder cb:创建一个查询条件</span></span><br><span class="line"><span class="comment">			 */</span></span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">				Predicate pre = cb.equal(root.get(<span class="string">"username"</span>), <span class="string">"王五"</span>);</span><br><span class="line">				<span class="keyword">return</span> pre;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;;</span><br><span class="line">		List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findAll(spec);</span><br><span class="line">		<span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">			System.out.println(users);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-多条件查询"><a href="#2-多条件查询" class="headerlink" title="2 多条件查询"></a>2 多条件查询</h2><h3 id="2-1给定查询条件方式一"><a href="#2-1给定查询条件方式一" class="headerlink" title="2.1给定查询条件方式一"></a>2.1给定查询条件方式一</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 多条件查询 方式一</span></span><br><span class="line"><span class="comment">  * 需求：使用用户姓名以及年龄查询数据</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">            List&lt;Predicate&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">            list.add(cb.equal(root.get(<span class="string">"username"</span>),<span class="string">"王五"</span>));</span><br><span class="line">            list.add(cb.equal(root.get(<span class="string">"userage"</span>),<span class="number">24</span>));</span><br><span class="line">            <span class="comment">//此时条件之间是没有任何关系的。</span></span><br><span class="line">            Predicate[] arr = <span class="keyword">new</span> Predicate[list.size()];</span><br><span class="line">            <span class="keyword">return</span> cb.and(list.toArray(arr));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line">    List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findAll(spec);</span><br><span class="line">    <span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-2-给定查询条件方式二"><a href="#2-2-给定查询条件方式二" class="headerlink" title="2.2 给定查询条件方式二"></a>2.2 给定查询条件方式二</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 多条件查询 方式二</span></span><br><span class="line"><span class="comment">  * 需求：使用用户姓名或者年龄查询数据</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;</span><br><span class="line">    Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> cb.or(cb.equal(root.get(<span class="string">"username"</span>),<span class="string">"王五"</span>),cb.equal(root.get(<span class="string">"userage"</span>), <span class="number">25</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line">    List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findAll(spec);</span><br><span class="line">    <span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-分页"><a href="#3-分页" class="headerlink" title="3 分页"></a>3 分页</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 需求：查询王姓用户，并且做分页处理</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//条件</span></span><br><span class="line">    Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">            return cb.like(root.get("username").as(String.class), "王%");</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//分页</span></span><br><span class="line">    Pageable pageable = <span class="keyword">new</span> PageRequest(<span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line">    Page&lt;Users&gt; page = <span class="keyword">this</span>.usersDao.findAll(spec, pageable);</span><br><span class="line">    System.out.println(<span class="string">"总条数："</span>+page.getTotalElements());</span><br><span class="line">    System.out.println(<span class="string">"总页数："</span>+page.getTotalPages());</span><br><span class="line">    List&lt;Users&gt; list = page.getContent();</span><br><span class="line">    <span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-排序"><a href="#4-排序" class="headerlink" title="4 排序"></a>4 排序</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 需求：查询数据库中王姓的用户，并且根据用户id做倒序排序</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//条件</span></span><br><span class="line">    Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">            return cb.like(root.get("username").as(String.class), "王%");</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//排序</span></span><br><span class="line">    Sort sort = <span class="keyword">new</span> Sort(Direction.DESC,<span class="string">"userid"</span>);</span><br><span class="line">    List&lt;Users&gt; list = <span class="keyword">this</span>.usersDao.findAll(spec, sort);</span><br><span class="line">    <span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-分页与排序"><a href="#5-分页与排序" class="headerlink" title="5 分页与排序"></a>5 分页与排序</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 需求：查询数据库中王姓的用户，做分页处理，并且根据用户id做倒序排序</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test6</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//排序等定义</span></span><br><span class="line">    Sort sort = <span class="keyword">new</span> Sort(Direction.DESC,<span class="string">"userid"</span>);</span><br><span class="line">    <span class="comment">//分页的定义</span></span><br><span class="line">    Pageable pageable = <span class="keyword">new</span> PageRequest(<span class="number">2</span>,<span class="number">2</span>, sort);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//查询条件</span></span><br><span class="line">    Specification&lt;Users&gt; spec = <span class="keyword">new</span> Specification&lt;Users&gt;() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Predicate <span class="title">toPredicate</span><span class="params">(Root&lt;Users&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb)</span> </span>&#123;</span><br><span class="line">            return cb.like(root.get("username").as(String.class), "王%");</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line">    Page&lt;Users&gt; page = <span class="keyword">this</span>.usersDao.findAll(spec, pageable);</span><br><span class="line">    System.out.println(<span class="string">"总条数："</span>+page.getTotalElements());</span><br><span class="line">    System.out.println(<span class="string">"总页数："</span>+page.getTotalPages());</span><br><span class="line">    List&lt;Users&gt; list = page.getContent();</span><br><span class="line">    <span class="keyword">for</span> (Users users : list) &#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="九、-用户自定义Repository接口"><a href="#九、-用户自定义Repository接口" class="headerlink" title="九、 用户自定义Repository接口"></a>九、 用户自定义Repository接口</h1><h2 id="创建接口-3"><a href="#创建接口-3" class="headerlink" title="创建接口"></a>创建接口</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersRepository</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Users <span class="title">findUserById</span><span class="params">(Integer userid)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="使用接口"><a href="#使用接口" class="headerlink" title="使用接口"></a>使用接口</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 用户自定义Repository接口讲解</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt;,<span class="title">JpaSpecificationExecutor</span>&lt;<span class="title">Users</span>&gt;,<span class="title">UsersRepository</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="创建接口实现类"><a href="#创建接口实现类" class="headerlink" title="创建接口实现类"></a>创建接口实现类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UsersDaoImpl</span> <span class="keyword">implements</span> <span class="title">UsersRepository</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@PersistenceContext</span>(name=<span class="string">"entityManagerFactory"</span>)</span><br><span class="line">	<span class="keyword">private</span> EntityManager em;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Users <span class="title">findUserById</span><span class="params">(Integer userid)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"MyRepository......"</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span>.em.find(Users<span class="class">.<span class="keyword">class</span>, <span class="title">userid</span>)</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="编写测试代码"><a href="#编写测试代码" class="headerlink" title="编写测试代码"></a>编写测试代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * JpaRepository接口测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RepositoryTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 需求：根据用户ID查询数据</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users users = <span class="keyword">this</span>.usersDao.findUserById(<span class="number">5</span>);</span><br><span class="line">		System.out.println(users);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="十、-关联映射操作"><a href="#十、-关联映射操作" class="headerlink" title="十、 关联映射操作"></a>十、 关联映射操作</h1><h2 id="1-一对一的关联关系"><a href="#1-一对一的关联关系" class="headerlink" title="1 一对一的关联关系"></a>1 一对一的关联关系</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">需求:用户与角色的一对一的关联关系</span><br><span class="line">用户:一方</span><br><span class="line">角色:一方</span><br></pre></td></tr></table></figure>

<h3 id="1-1创建-Users-实体"><a href="#1-1创建-Users-实体" class="headerlink" title="1.1创建 Users 实体"></a>1.1创建 Users 实体</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="meta">@Table</span>(name=<span class="string">"t_users"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Users</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Id</span></span><br><span class="line">    <span class="comment">//strategy=GenerationType.IDENTITY 自增长</span></span><br><span class="line">	<span class="meta">@GeneratedValue</span>(strategy=GenerationType.IDENTITY)</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"userid"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer userid;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"username"</span>)</span><br><span class="line">	<span class="keyword">private</span> String username;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"userage"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer userage;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Roles <span class="title">getRoles</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> roles;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRoles</span><span class="params">(Roles roles)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.roles = roles;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@OneToOne</span>(cascade=CascadeType.PERSIST)</span><br><span class="line">	<span class="comment">//@JoinColumn：就是维护一个外键</span></span><br><span class="line">	<span class="meta">@JoinColumn</span>(name=<span class="string">"roles_id"</span>)</span><br><span class="line">	<span class="keyword">private</span> Roles roles;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getUserid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> userid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserid</span><span class="params">(Integer userid)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.userid = userid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getUsername</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUsername</span><span class="params">(String username)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.username = username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getUserage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> userage;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserage</span><span class="params">(Integer userage)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.userage = userage;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Users [userid="</span> + userid + <span class="string">", username="</span> + username + <span class="string">", userage="</span> + userage + <span class="string">"]"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-2创建-Roles-实体"><a href="#1-2创建-Roles-实体" class="headerlink" title="1.2创建 Roles 实体"></a>1.2创建 Roles 实体</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="meta">@Table</span>(name=<span class="string">"t_roles"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Roles</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Id</span></span><br><span class="line">	<span class="meta">@GeneratedValue</span>(strategy=GenerationType.IDENTITY)</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"roleid"</span>)</span><br><span class="line">	<span class="keyword">private</span> Integer roleid;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Column</span>(name=<span class="string">"rolename"</span>)</span><br><span class="line">	<span class="keyword">private</span> String rolename;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@OneToOne</span>(mappedBy=<span class="string">"roles"</span>)</span><br><span class="line">	<span class="keyword">private</span> Users users;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Users <span class="title">getUsers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> users;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUsers</span><span class="params">(Users users)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.users = users;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getRoleid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> roleid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRoleid</span><span class="params">(Integer roleid)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.roleid = roleid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getRolename</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> rolename;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRolename</span><span class="params">(String rolename)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.rolename = rolename;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"Roles [roleid="</span> + roleid + <span class="string">", rolename="</span> + rolename + <span class="string">"]"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3一对一关联关系操作"><a href="#1-3一对一关联关系操作" class="headerlink" title="1.3一对一关联关系操作"></a>1.3一对一关联关系操作</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 一对一关联关系测试</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">@ContextConfiguration("classpath:applicationContext.xml")</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OneToOneTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UsersDao usersDao;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 添加用户同时添加角色</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//创建角色</span></span><br><span class="line">		Roles roles = <span class="keyword">new</span> Roles();</span><br><span class="line">		roles.setRolename(<span class="string">"管理员"</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//创建用户</span></span><br><span class="line">		Users users = <span class="keyword">new</span> Users();</span><br><span class="line">		users.setUserage(<span class="number">30</span>);</span><br><span class="line">		users.setUsername(<span class="string">"赵小刚"</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//建立关系</span></span><br><span class="line">		users.setRoles(roles);</span><br><span class="line">		roles.setUsers(users);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//保存数据</span></span><br><span class="line">		<span class="keyword">this</span>.usersDao.save(users);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 根据用户ID查询用户，同时查询用户角色</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">		Users users = <span class="keyword">this</span>.usersDao.findOne(<span class="number">13</span>);</span><br><span class="line">		System.out.println(<span class="string">"用户信息："</span>+users);</span><br><span class="line">		Roles roles = users.getRoles();</span><br><span class="line">		System.out.println(roles);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="接口-2"><a href="#接口-2" class="headerlink" title="接口"></a>接口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 一对一关联关系操作</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Administrator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UsersDao</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">Users</span>, <span class="title">Integer</span>&gt;,<span class="title">JpaSpecificationExecutor</span>&lt;<span class="title">Users</span>&gt;</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2、-OneToMany-和-ManyToOne"><a href="#2、-OneToMany-和-ManyToOne" class="headerlink" title="2、@OneToMany 和 @ManyToOne"></a>2、<strong>@OneToMany 和 @ManyToOne</strong></h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">需求:从角色到用户的一对多的关联关系</span><br><span class="line">角色:一方</span><br><span class="line">用户:多方</span><br></pre></td></tr></table></figure>

<h3 id="2-1-OneToMany"><a href="#2-1-OneToMany" class="headerlink" title="2.1 @OneToMany:"></a>2.1 @OneToMany:</h3><p>作用：<br>   建立一对多的关系映射<br>属性：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">targetEntityClass：指定多的多方的类的字节码</span><br><span class="line">mappedBy：指定从表实体类中引用主表对象的名称。</span><br><span class="line">cascade：指定要使用的级联操作</span><br><span class="line">fetch：指定是否采用延迟加载</span><br><span class="line">orphanRemoval：是否使用孤儿删除</span><br></pre></td></tr></table></figure>

<h3 id="2-2-ManyToOne"><a href="#2-2-ManyToOne" class="headerlink" title="2.2 @ManyToOne:"></a>2.2 @ManyToOne:</h3><p>作用：<br>  建立多对一的关系<br>属性：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">targetEntityClass：指定一的一方实体类字节码</span><br><span class="line">cascade：指定要使用的级联操作</span><br><span class="line">fetch：指定是否采用延迟加载</span><br><span class="line">optional：关联是否可选。如果设置为false，则必须始终存在非空关系。</span><br></pre></td></tr></table></figure>

<h3 id="2-3-JoinColumn"><a href="#2-3-JoinColumn" class="headerlink" title="2.3 @JoinColumn"></a>2.3 @JoinColumn</h3><p>作用：<br>  用于定义主键字段和外键字段的对应关系。<br>属性：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">name：指定外键字段的名称</span><br><span class="line">referencedColumnName：指定引用主表的主键字段名称</span><br><span class="line">unique：是否唯一。默认值不唯一</span><br><span class="line">nullable：是否允许为空。默认值允许。</span><br><span class="line">insertable：是否允许插入。默认值允许。</span><br><span class="line">updatable：是否允许更新。默认值允许。</span><br><span class="line">columnDefinition：列的定义信息。</span><br></pre></td></tr></table></figure>

<p>实体 Author：作者。</p>
<p>实体 Article：文章。</p>
<p>Author 和 Article 是一对多关系(双向)。那么在JPA中，如何表示一对多的双向关联呢？</p>
<p>JPA使用@OneToMany和@ManyToOne来标识一对多的双向关联。一端(Author)使用@OneToMany,多端(Article)使用@ManyToOne。</p>
<p>在JPA规范中，一对多的双向关系由多端(Article)来维护。就是说多端(Article)为关系维护端，负责关系的增删改查。一端(Author)则为关系被维护端，不能维护关系。</p>
<p>一端(Author)使用@OneToMany注释的mappedBy=”author”属性表明Author是关系被维护端。</p>
<p>多端(Article)使用@ManyToOne和@JoinColumn来注释属性 author,@ManyToOne表明Article是多端，@JoinColumn设置在article表中的关联字段(外键)。</p>
<p>Author.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Author</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 主键</span></span><br><span class="line">    <span class="meta">@Id</span> </span><br><span class="line">    <span class="comment">// 自增长策略</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span>(strategy = GenerationType.IDENTITY) </span><br><span class="line">    <span class="comment">//id</span></span><br><span class="line">    <span class="keyword">private</span> Long id; </span><br><span class="line"></span><br><span class="line">    <span class="meta">@NotEmpty</span>(message = <span class="string">"姓名不能为空"</span>)</span><br><span class="line">    <span class="meta">@Size</span>(min=<span class="number">2</span>, max=<span class="number">20</span>)</span><br><span class="line">    <span class="meta">@Column</span>(nullable = <span class="keyword">false</span>, length = <span class="number">20</span>)</span><br><span class="line">    <span class="keyword">private</span> String name;<span class="comment">//姓名</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@OneToMany</span>(mappedBy = <span class="string">"author"</span>,cascade=CascadeType.ALL,fetch=FetchType.LAZY)</span><br><span class="line">    <span class="comment">//级联保存、更新、删除、刷新;延迟加载。当删除用户，会级联删除该用户的所有文章</span></span><br><span class="line">    <span class="comment">//拥有mappedBy注解的实体类为关系被维护端</span></span><br><span class="line">    <span class="comment">//mappedBy="author"中的author是Article中的author属性</span></span><br><span class="line">    <span class="comment">//文章列表 </span></span><br><span class="line">    <span class="keyword">private</span> List&lt;Article&gt; articleList;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>Article.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Article</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="comment">// 自增长策略</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span>(strategy = GenerationType.IDENTITY) </span><br><span class="line">    <span class="meta">@Column</span>(name = <span class="string">"id"</span>, nullable = <span class="keyword">false</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@NotEmpty</span>(message = <span class="string">"标题不能为空"</span>)</span><br><span class="line">    <span class="meta">@Size</span>(min = <span class="number">2</span>, max = <span class="number">50</span>)</span><br><span class="line">    <span class="comment">// 映射为字段，值不能为空</span></span><br><span class="line">    <span class="meta">@Column</span>(nullable = **<span class="keyword">false</span>**, length = <span class="number">50</span>) </span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String title;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 大对象，映射 MySQL 的 Long Text 类型</span></span><br><span class="line">    <span class="meta">@Lob</span></span><br><span class="line">    <span class="comment">// 懒加载</span></span><br><span class="line">    <span class="meta">@Basic</span>(fetch = FetchType.LAZY) </span><br><span class="line">    <span class="meta">@NotEmpty</span>(message = <span class="string">"内容不能为空"</span>)</span><br><span class="line">    <span class="meta">@Size</span>(min = <span class="number">2</span>)</span><br><span class="line">    <span class="comment">// 映射为字段，值不能为空</span></span><br><span class="line">    <span class="meta">@Column</span>(nullable = <span class="keyword">false</span>) </span><br><span class="line">    <span class="comment">//文章全文内容</span></span><br><span class="line">    <span class="keyword">private</span> String content;</span><br><span class="line">    <span class="comment">//可选属性optional=false,表示author不能为空。删除文章，不影响用户</span></span><br><span class="line">    <span class="meta">@ManyToOne</span>(cascade=&#123;CascadeType.MERGE,CascadeType.REFRESH&#125;,optional=<span class="keyword">false</span>)</span><br><span class="line">    <span class="comment">//设置在article表中的关联字段(外键)</span></span><br><span class="line">    <span class="meta">@JoinColumn</span>(name=<span class="string">"author_id"</span>)</span><br><span class="line">    <span class="comment">//所属作者</span></span><br><span class="line">    <span class="keyword">private</span> Author author;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最终生成的表结构</p>
<p>article 表(id，title，conten，<strong>author_id</strong>)</p>
<p>author 表(<strong>id</strong>，name)</p>
<h2 id="3、多对多-ManyToMany"><a href="#3、多对多-ManyToMany" class="headerlink" title="3、多对多 @ManyToMany"></a>3、多对多 @ManyToMany</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">需求:一个角色可以拥有多个菜单，一个菜单可以分配多个角色。多对多的关联关系 角色:多方</span><br><span class="line">菜单:多方</span><br></pre></td></tr></table></figure>

<p>实体 User：用户。</p>
<p>实体 Authority：权限。</p>
<p>用户和权限是多对多的关系。一个用户可以有多个权限，一个权限也可以被很多用户拥有。</p>
<p>JPA中使用@ManyToMany来注解多对多的关系，由一个关联表来维护。这个关联表的表名默认是：主表名+下划线+从表名。(主表是指关系维护端对应的表,从表指关系被维护端对应的表)。这个关联表只有两个外键字段，分别指向主表ID和从表ID。字段的名称默认为：主表名+下划线+主表中的主键列名，从表名+下划线+从表中的主键列名。</p>
<p>需要注意的：</p>
<p>1、多对多关系中一般不设置级联保存、级联删除、级联更新等操作。</p>
<p>2、可以随意指定一方为关系维护端，在这个例子中，我指定 User 为关系维护端，所以生成的关联表名称为： user_authority，关联表的字段为：user_id 和 authority_id。</p>
<p>3、多对多关系的绑定由关系维护端来完成，即由 User.setAuthorities(authorities) 来绑定多对多的关系。关系被维护端不能绑定关系，即Game不能绑定关系。</p>
<p>4、多对多关系的解除由关系维护端来完成，即由Player.getGames().remove(game)来解除多对多的关系。关系被维护端不能解除关系，即Game不能解除关系。</p>
<p>5、如果 User 和 Authority 已经绑定了多对多的关系，那么不能直接删除 Authority，需要由 User 解除关系后，才能删除 Authority。但是可以直接删除 User，因为 User 是关系维护端，删除 User 时，会先解除 User 和 Authority 的关系，再删除 Authority。</p>
<p>User.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span>(strategy = GenerationType.IDENTITY)</span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line">    <span class="meta">@NotEmpty</span>(message = <span class="string">"账号不能为空"</span>)</span><br><span class="line">    <span class="meta">@Size</span>(min=<span class="number">3</span>, max=<span class="number">20</span>)</span><br><span class="line">    <span class="meta">@Column</span>(nullable = <span class="keyword">false</span>, length = <span class="number">20</span>, unique = <span class="keyword">true</span>)</span><br><span class="line">    <span class="comment">// 用户账号，用户登录时的唯一标识</span></span><br><span class="line">    <span class="keyword">private</span> String username; </span><br><span class="line">    <span class="meta">@NotEmpty</span>(message = <span class="string">"密码不能为空"</span>)</span><br><span class="line">    <span class="meta">@Size</span>(max=<span class="number">100</span>)</span><br><span class="line">    <span class="meta">@Column</span>(length = <span class="number">100</span>)</span><br><span class="line">    <span class="comment">// 登录时密码</span></span><br><span class="line">    <span class="keyword">private</span> String password; </span><br><span class="line">    <span class="meta">@ManyToMany</span></span><br><span class="line">    <span class="meta">@JoinTable</span>(name = <span class="string">"user_authority"</span>,joinColumns = <span class="meta">@JoinColumn</span>(name = <span class="string">"user_id"</span>),</span><br><span class="line">    inverseJoinColumns = <span class="meta">@JoinColumn</span>(name = <span class="string">"authority_id"</span>))</span><br><span class="line">    <span class="comment">//1、关系维护端，负责多对多关系的绑定和解除</span></span><br><span class="line">    <span class="comment">//2、@JoinTable注解的name属性指定关联表的名字，joinColumns指定外键的名字，关联到关系维护端(User)</span></span><br><span class="line">    <span class="comment">//3、inverseJoinColumns指定外键的名字，要关联的关系被维护端(Authority)</span></span><br><span class="line">    <span class="comment">//4、其实可以不使用@JoinTable注解，默认生成的关联表名称为主表表名+下划线+从表表名，</span></span><br><span class="line">    <span class="comment">//即表名为user_authority</span></span><br><span class="line">    <span class="comment">//关联到主表的外键名：主表名+下划线+主表中的主键列名,即user_id</span></span><br><span class="line">    <span class="comment">//关联到从表的外键名：主表中用于关联的属性名+下划线+从表的主键列名,即authority_id</span></span><br><span class="line">    <span class="comment">//主表就是关系维护端对应的表，从表就是关系被维护端对应的表</span></span><br><span class="line">    <span class="keyword">private</span> List&lt;Authority&gt; authorityList;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：如注释中所言，上面的第20-21行的@JoinTable可以省略，默认可以生成</p>
<p>Authority.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Authority</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span>(strategy = GenerationType.IDENTITY)</span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line">    <span class="meta">@Column</span>(nullable = <span class="keyword">false</span>)</span><br><span class="line">    <span class="comment">//权限名</span></span><br><span class="line">    <span class="keyword">private</span> String name; </span><br><span class="line">    <span class="meta">@ManyToMany</span>(mappedBy = <span class="string">"authorityList"</span>)</span><br><span class="line">    <span class="keyword">private</span> List&lt;User&gt; userList;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    
    <div>    
      
        <ul class="post-copyright">
          <li class="post-copyright-link">
          <strong>本文作者：</strong>
          <a href="/" title="欢迎访问 Chenyumeng 的个人博客">Chenyumeng</a>
          </li>

          <li class="post-copyright-link">
            <strong>本文标题：</strong>
            <a href="https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/" title="Spring Data JPA的基本使用">Spring Data JPA的基本使用</a>
          </li>

          <li class="post-copyright-link">
            <strong>本文链接：</strong>
            <a href="https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/" title="Spring Data JPA的基本使用">https://ipvb.gitee.io/blog/2020/04/23/SpringDataJPA/</a>
          </li>

          <li class="post-copyright-date">
            <strong>发布时间： </strong>2020年4月23日 - 18时04分
          </li>  

          <li class="post-copyright-license">
            <strong>版权声明： </strong>
            本文由 Chenyumeng 原创。
          </li>
        </ul>
      
    </div>
      
  <div class="popular-posts-header">相关文章推荐</div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\blog\2020\05\23\Java NIO Channel\" rel="bookmark">Java NIO Channel</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\blog\2020\05\22\Java NIO Buffer\" rel="bookmark">Java NIO Buffer</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\blog\2020\05\23\Java NIO Selector\" rel="bookmark">Java NIO Selector</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\blog\2020\03\15\ZooKeeper\" rel="bookmark">ZooKeeper单机与集群安装笔记</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\blog\2020\03\15\centOs7_Redis\" rel="bookmark">centOs7下安装Redis记录</a></div>
    </li>
  </ul>

        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/blog/images/wechatpay.png" alt="Chenyumeng 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/blog/images/alipay.png" alt="Chenyumeng 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------　　　　本文结束　<i class="fa fa-heart"></i>　感谢您的阅读　　　　-------------</div>
    
</div>
        
      </div>

      <footer class="post-footer">
          
          <div class="post-tags">
            
              <a href="/blog/" rel="tag"><i class="fa fa-tag"></i> </a>
            
              <a href="/blog/" rel="tag"><i class="fa fa-tag"></i> </a>
            
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/04/23/2020%E5%B9%B404%E6%9C%8823%E6%97%A5/" rel="prev" title="2020年04月23日软件设计师每日一练">
      <i class="fa fa-chevron-left"></i> 2020年04月23日软件设计师每日一练
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/04/24/2020%E5%B9%B404%E6%9C%8824%E6%97%A5/" rel="next" title="2020年04月24日软件设计师每日一练">
      2020年04月24日软件设计师每日一练 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

    <!-- 引入share.js -->
    
      <div data-weibo-title="分享到微博" data-qq-title="分享到QQ" data-douban-title="分享到豆瓣" class="social-share" class="share-component" data-disabled="twitter,facebook" data-description="Share.js - 一键分享到微博，QQ空间，腾讯微博，人人，豆瓣">分享到：</div>
    
  </div>


          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC80OTExNy8yNTYxMg=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Spring-Data-JPA-基本使用"><span class="nav-number">1.</span> <span class="nav-text">Spring Data JPA 基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-Data-JPA"><span class="nav-number">1.1.</span> <span class="nav-text">Spring Data JPA:</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-Data-JPA-的技术特点"><span class="nav-number">1.2.</span> <span class="nav-text">Spring Data JPA 的技术特点:</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#一、-创建SpringDataJPA项目"><span class="nav-number">2.</span> <span class="nav-text">一、 创建SpringDataJPA项目</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-导入依赖"><span class="nav-number">2.1.</span> <span class="nav-text">1 导入依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-配置数据源信息"><span class="nav-number">2.2.</span> <span class="nav-text">2 配置数据源信息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-编写Dao"><span class="nav-number">2.3.</span> <span class="nav-text">3 编写Dao</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-User"><span class="nav-number">2.4.</span> <span class="nav-text">4 User</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-编写测试代码"><span class="nav-number">2.5.</span> <span class="nav-text">5 编写测试代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、-Spring-Data-JPA-的接口继承结构"><span class="nav-number">3.</span> <span class="nav-text">二、 Spring Data JPA 的接口继承结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、-Spring-Data-JPA-的运行原理"><span class="nav-number">4.</span> <span class="nav-text">三、 Spring Data JPA 的运行原理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#四、-Repository-接口"><span class="nav-number">5.</span> <span class="nav-text">四、 Repository 接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-方法名称命名规则查询"><span class="nav-number">5.1.</span> <span class="nav-text">1 方法名称命名规则查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建接口"><span class="nav-number">5.1.0.1.</span> <span class="nav-text">创建接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#测试类"><span class="nav-number">5.1.0.2.</span> <span class="nav-text">测试类</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-基于-Query-注解的查询"><span class="nav-number">5.2.</span> <span class="nav-text">2 基于@Query 注解的查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1通过-JPQL-语句查询"><span class="nav-number">5.2.1.</span> <span class="nav-text">2.1通过 JPQL 语句查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建接口-1"><span class="nav-number">5.2.1.1.</span> <span class="nav-text">创建接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#测试类-1"><span class="nav-number">5.2.1.2.</span> <span class="nav-text">测试类</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2通过-SQL-语句查询"><span class="nav-number">5.3.</span> <span class="nav-text">2.2通过 SQL 语句查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#接口"><span class="nav-number">5.3.0.1.</span> <span class="nav-text">接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#测试类-2"><span class="nav-number">5.3.0.2.</span> <span class="nav-text">测试类</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-通过-Query-注解完成数据更新"><span class="nav-number">5.3.1.</span> <span class="nav-text">3 通过@Query 注解完成数据更新</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#接口-1"><span class="nav-number">5.3.1.1.</span> <span class="nav-text">接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#测试类-3"><span class="nav-number">5.3.1.2.</span> <span class="nav-text">测试类</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#五、-CrudRepository-接口"><span class="nav-number">6.</span> <span class="nav-text">五、 CrudRepository 接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-创建接口"><span class="nav-number">6.1.</span> <span class="nav-text">1 创建接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-测试代码"><span class="nav-number">6.2.</span> <span class="nav-text">2 测试代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#六、-PagingAndSortingRepository-接口"><span class="nav-number">7.</span> <span class="nav-text">六、 PagingAndSortingRepository 接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-分页处理"><span class="nav-number">7.1.</span> <span class="nav-text">1 分页处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1创建接口"><span class="nav-number">7.1.1.</span> <span class="nav-text">1.1创建接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2测试代码"><span class="nav-number">7.1.2.</span> <span class="nav-text">1.2测试代码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-排序的处理"><span class="nav-number">7.2.</span> <span class="nav-text">2 排序的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1测试代码"><span class="nav-number">7.2.1.</span> <span class="nav-text">2.1测试代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#七、-JpaRepository-接口"><span class="nav-number">8.</span> <span class="nav-text">七、 JpaRepository 接口</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建接口-2"><span class="nav-number">8.0.0.1.</span> <span class="nav-text">创建接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#测试类-4"><span class="nav-number">8.0.0.2.</span> <span class="nav-text">测试类</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#八、-JpaSpecificationExecutor-接口"><span class="nav-number">9.</span> <span class="nav-text">八、 JpaSpecificationExecutor 接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-单条件查询"><span class="nav-number">9.1.</span> <span class="nav-text">1 单条件查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1创建接口-1"><span class="nav-number">9.1.1.</span> <span class="nav-text">1.1创建接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试接口"><span class="nav-number">9.1.2.</span> <span class="nav-text">测试接口</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-多条件查询"><span class="nav-number">9.2.</span> <span class="nav-text">2 多条件查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1给定查询条件方式一"><span class="nav-number">9.2.1.</span> <span class="nav-text">2.1给定查询条件方式一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-给定查询条件方式二"><span class="nav-number">9.2.2.</span> <span class="nav-text">2.2 给定查询条件方式二</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-分页"><span class="nav-number">9.3.</span> <span class="nav-text">3 分页</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-排序"><span class="nav-number">9.4.</span> <span class="nav-text">4 排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-分页与排序"><span class="nav-number">9.5.</span> <span class="nav-text">5 分页与排序</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#九、-用户自定义Repository接口"><span class="nav-number">10.</span> <span class="nav-text">九、 用户自定义Repository接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#创建接口-3"><span class="nav-number">10.1.</span> <span class="nav-text">创建接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用接口"><span class="nav-number">10.2.</span> <span class="nav-text">使用接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建接口实现类"><span class="nav-number">10.3.</span> <span class="nav-text">创建接口实现类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#编写测试代码"><span class="nav-number">10.4.</span> <span class="nav-text">编写测试代码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#十、-关联映射操作"><span class="nav-number">11.</span> <span class="nav-text">十、 关联映射操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-一对一的关联关系"><span class="nav-number">11.1.</span> <span class="nav-text">1 一对一的关联关系</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1创建-Users-实体"><span class="nav-number">11.1.1.</span> <span class="nav-text">1.1创建 Users 实体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2创建-Roles-实体"><span class="nav-number">11.1.2.</span> <span class="nav-text">1.2创建 Roles 实体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3一对一关联关系操作"><span class="nav-number">11.1.3.</span> <span class="nav-text">1.3一对一关联关系操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#接口-2"><span class="nav-number">11.1.4.</span> <span class="nav-text">接口</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、-OneToMany-和-ManyToOne"><span class="nav-number">11.2.</span> <span class="nav-text">2、@OneToMany 和 @ManyToOne</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-OneToMany"><span class="nav-number">11.2.1.</span> <span class="nav-text">2.1 @OneToMany:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-ManyToOne"><span class="nav-number">11.2.2.</span> <span class="nav-text">2.2 @ManyToOne:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-JoinColumn"><span class="nav-number">11.2.3.</span> <span class="nav-text">2.3 @JoinColumn</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、多对多-ManyToMany"><span class="nav-number">11.3.</span> <span class="nav-text">3、多对多 @ManyToMany</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Chenyumeng"
      src="/blog/images/touxiang.JPG">
  <p class="site-author-name" itemprop="name">Chenyumeng</p>
  <div class="site-description" itemprop="description">用来记录自己学习中所遇到的问题以及如何解决和自己所学知识的理解</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">29</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chen-yumeng" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chen-yumeng" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/3123221441" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;3123221441" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://github.com/" title="https:&#x2F;&#x2F;github.com&#x2F;" rel="noopener" target="_blank">Github</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chenyumeng</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">390k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:55</span>
</div>

        






  <script>
  function leancloudSelector(url) {
    url = encodeURI(url);
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = decodeURI(visitors.id);
      var title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .catch(error => {
                console.error('Failed to save visitor count', error);
              })
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            let target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : '',
            'X-LC-Key'    : '',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (CONFIG.hostname !== location.hostname) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>


      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

  <script src="/dist/js/social-share.min.js"></script>
</body>
</html>
